/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div>
      <div class="fixed-bar">
        <div class="item-title">
          <router-link
            class="back"
            :to="{name:toLink, params:{currentPage:$route.params.currentPage}}"
            title="返回列表"
          >
            <i class="fa fa-arrow-circle-o-left"></i>
          </router-link>
          <div class="subject">
            <h3>
              <span v-if="isOwnShop == 1">自营店铺</span>
              <span v-else>店铺</span>
              - 新增
            </h3>
            <h5>商城店铺相关设置与管理</h5>
          </div>
        </div>
        <div
          id="explanation"
          class="explanation"
          style="color: rgb(85, 85, 85); background-color: rgb(243, 244, 244); width: 98%; height: 100%;"
        >
          <div class="bckopa-tips">
            <div class="title">
              <img src="@/static/images/handd.png" alt />
              <h4 title="提示相关设置操作时应注意的要点">操作提示</h4>
            </div>
            <ul>
              <li>1. 平台可以在此处添加店铺，新增的店铺默认为开启状态</li>
              <li>2. 新增店铺默认绑定所有经营类目并且佣金为0，可以手动设置绑定其经营类目。</li>
              <li>3. 新增店铺将自动创建店主会员账号（用于登录网站会员中心）以及商家账号（用于登录商家中心）。</li>
            </ul>
          </div>
          <span title="收起提示" id="explanationZoom" style="display: block;"></span>
        </div>
      </div>
      <el-form ref="form" :model="form" label-width="200px">
        <el-form-item label="店铺名称" required>
          <el-input v-model="form.store_name"></el-input>
        </el-form-item>
        <el-form-item label="会员账号" required>
          <el-input v-model="form.account"></el-input>
          <p class="err-msg" v-if="errResult.account">{{errResult.account}}</p>
          <p class="notic">请输入手机号或者邮箱，用于登录会员中心</p>
        </el-form-item>
        <el-form-item label="商家账号" required>
          <el-input v-model="form.seller_name"></el-input>
          <p class="notic">用于登录商家中心，可与店主账号不同</p>
        </el-form-item>
        <el-form-item label="登陆密码" required>
          <el-input type="password" v-model="password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" :loading="loading">确认提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { addStore } from "@/utils/api";
import md5 from "@/utils/md5.js";
export default {
  data() {
    return {
      form: {},
      isOwnShop: this.$route.params.isOwnShop,
      toLink: this.$route.params.isOwnShop == 0 ? "stores" : "own_stores",
      loading: false,
      password: null,
      errResult: {}
    };
  },
  methods: {
    onSubmit() {
      if (
        !this.form.store_name ||
        !this.form.account ||
        !this.form.seller_name ||
        !this.password
      ) {
        this.$message.error("请填写完整表单");
        return;
      }
      this.form.is_own_shop = this.$route.params.isOwnShop;
      //判断账号类型 邮箱或手机号
      var mobileReg = /^1\d{10}$/;
      var emailReg = /^([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+.[a-zA-Z]{2,3}$/;
      if (mobileReg.test(this.form.account)) {
        this.form.mobile = this.form.account;
      } else if (emailReg.test(this.form.account)) {
        this.form.email = this.form.account;
      } else {
        this.$message.error("请填写合法的会员账号");
        return;
      }
      if (this.password.length < 6 || this.password.length > 16) {
        this.$message.error("密码长度为6-16位");
        return;
      }
      this.form.password = md5("TPSHOP" + this.password);
      this.loading = true;
      var that = this;
      addStore(that.form).then(function(res) {
        if (res.status == 1) {
          that.$message.success({
            message: "添加成功",
            duration: 1000,
            onClose: function() {
              that.$router.push({ name: that.toLink });
            }
          });
        } else {
          that.$message.error(res.msg);
        }
        that.loading = false;
      });
    }
  }
};
</script>

<style scoped>
</style>
